package edu.ccut.saturn.studio.logic.model.drop;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnLogicNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;

public class TestDorp {

	private static final String LOGIC_DROP_HANDLER_TWO_DOT = "::";
	private static final String LOGIC_DROP_HANDLER_DESCRIPTION = "description";
	private static final String LOGIC_DROP_HANDLER_NAME = "name";
	private static final String LOGIC_DROP_HANDLER_KEY = "key";
	private static final String LOGIC_DROP_HANDLER_INFORMATION = "information";
	public static final String FILE_POSTFIX = ".logic";
	private static final String COMPONENT_DROP_HANDLER_NULL = "";
	private String bundleName;
	public TestDorp(String bundleName){
		this.bundleName = bundleName;
	}
	@SuppressWarnings("unchecked")
	public Shape getShape(Document doc) {

		try {
			Node root = doc.getDocumentElement();

			List<SaturnParameter> parameters = null;

			if (true) {
				List<Node> informatton = SaturnW3CUtil.getChildrenByName(
						root, LOGIC_DROP_HANDLER_INFORMATION);

				String keyValue = "";
				String name = "";
				String description = "";
				
				// //////////// for INF .mf
//
//				String workspaceString = ResourcesPlugin.getWorkspace()
//						.getRoot().getLocation().toOSString();
				//String si = filePath.replace(workspaceString, "");
				//String aaa[] = si.split(NEW_ACTION_WIZARD_SPLIT);
				//String projectName = aaa[1];
				//symbolicName = getBundleName(projectName);

				// /////////////////////////

				for (int j = 0; j < informatton.size(); j++) {
					Node in = (Node) informatton.get(j);

					for (Node node = in.getFirstChild(); node != null; node = node
							.getNextSibling()) {
						if (node.getNodeType() == Node.ELEMENT_NODE) {
							try {
								if (node.getNodeName().equals(
										LOGIC_DROP_HANDLER_KEY)) {
									keyValue = node.getFirstChild()
											.getNodeValue();
								}
							} catch (Exception e) {
							}
							try {
								if (node.getNodeName().equals(
										LOGIC_DROP_HANDLER_NAME)) {
									name = node.getFirstChild().getNodeValue();
								}
							} catch (Exception e) {
							}
							try {
								if (node.getNodeName().equals(
										LOGIC_DROP_HANDLER_DESCRIPTION)) {

									description = node.getFirstChild()
											.getNodeValue();
								}
							} catch (Exception e) {
							}
						}
					}
				}

				// ///////////////////////////////////
				try {
				List<Node> startNodes = SaturnW3CUtil.getChildrenByName(root,
						"start");
				for (int j = 0; j < startNodes.size(); j++) {
					Node in = (Node) startNodes.get(j);
					parameters = this.getParameters(in);
				}
				} catch (Exception e) {
					 // TODO : I can do late. for XML
				}
				SaturnLogicNode logicNode = new SaturnLogicNode();
				logicNode.setDescription(description);
				logicNode.setKey(bundleName.trim()
						+ LOGIC_DROP_HANDLER_TWO_DOT.trim() + keyValue.trim());
				logicNode.setName(name);
				logicNode.setParameters(parameters);

				return logicNode;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	// /////////////////////////
	protected List<SaturnParameter> getParameters(Node node) {
		Node parametersNode = this
				.getChildNode(node, Shape.KEY_NODE_PARAMETERS);

		List<SaturnParameter> parameters = new ArrayList<SaturnParameter>();

		if (parametersNode != null) {
			List<Node> parameterNodes = this.getChildNodes(parametersNode,
					Shape.KEY_NODE_PARAMETER);

			for (Node parameterNode : parameterNodes) {
				parameters.add(this.getParameter(parameterNode));
			}
		}

		return parameters;
	}

	protected Node getChildNode(Node parentNode, String nodeKey) {
		NodeList children = parentNode.getChildNodes();

		for (int i = 0; i < children.getLength(); ++i) {
			Node child = children.item(i);

			if (child.getNodeName().equals(nodeKey)) {
				return child;
			}
		}

		return null;
	}

	protected List<Node> getChildNodes(Node parentNode, String nodeKey) {
		NodeList children = parentNode.getChildNodes();

		List<Node> childNodes = new ArrayList<Node>();

		for (int i = 0; i < children.getLength(); ++i) {
			Node child = children.item(i);

			if (child.getNodeName().equals(nodeKey)) {
				childNodes.add(child);
			}
		}

		return childNodes;
	}

	private SaturnParameter getParameter(Node parameterNode) {
		String id = this.getNodeAttributeValue(parameterNode,
				Shape.KEY_PARAMETER_ATTRIBUTE_ID);
		String name = this.getNodeAttributeValue(parameterNode,
				Shape.KEY_PARAMETER_ATTRIBUTE_NAME);
		String classType = this.getNodeAttributeValue(parameterNode,
				Shape.KEY_PARAMETER_ATTRIBUTE_CLASS_TYPE);
		String passingType = this.getNodeAttributeValue(parameterNode,
				Shape.KEY_PARAMETER_ATTRIBUTE_PASSING_TYPE);

		String valueKey = this.getNodeElementValue(parameterNode,
				Shape.KEY_PARAMETER_NODE_VALUE_KEY);
		String scope = this.getNodeElementValue(parameterNode,
				Shape.KEY_PARAMETER_NODE_SCOPE);
		String description = this.getNodeElementValue(parameterNode,
				Shape.KEY_PARAMETER_NODE_DESCRIPTION);

		SaturnParameter saturnParameter = new SaturnParameter();
		saturnParameter.setId(id);
		saturnParameter.setName(name);
		saturnParameter.setClassType(classType);
		saturnParameter.setPassingType(passingType);
		saturnParameter.setValueKey(valueKey);
		saturnParameter.setScope(scope);
		saturnParameter.setDescription(description);

		return saturnParameter;
	}

	protected String getNodeAttributeValue(Node node, String key) {
		Node attribute = node.getAttributes().getNamedItem(key);

		if (attribute != null) {
			return attribute.getNodeValue();
		}

		return COMPONENT_DROP_HANDLER_NULL;
	}

	protected String getNodeElementValue(Node node, String key) {
		NodeList childNodes = node.getChildNodes();

		for (int i = 0; i < childNodes.getLength(); ++i) {
			Node child = childNodes.item(i);
			if (child.getNodeName().equals(key)) {
				if (child.getFirstChild() != null) {
					return child.getFirstChild().getNodeValue();
				}

				return COMPONENT_DROP_HANDLER_NULL;
			}
		}

		return COMPONENT_DROP_HANDLER_NULL;
	}


}
